﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.helper;
using WindowsFormsApp1.model;

namespace WindowsFormsApp1
{
    public partial class JobAddForm : Form
    {
        public JobAddForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShiGongTianChuangModel job = new ShiGongTianChuangModel();
            job.JobName = tbxJobName.Text.Trim();
            job.JobAddress = cbBJobAddress.Text.Trim();
            job.JobNature = cbBNature.Text.Trim();
            job.JobSubjectCompany = cbBSubjectCompany.Text.Trim();
            job.JobObjectCompany = new List<string>();
            for (int i = 0; i < cLBxObjectCompany.Items.Count; i++)
            {
                if (cLBxObjectCompany.GetItemChecked(i))
                {
                    job.JobObjectCompany.Add(cLBxObjectCompany.GetItemText(cLBxObjectCompany.Items[i]));
                }
            }

            if (cbBNature.Text.Trim() == "天窗")
            {
                job.JobIndex = GuidTo16String();
            }
            else
            {
                job.JobIndex = tBxJobindex.Text.Trim();
            }

            //添加流程
            job.JobTechnologicalProcess = new List<string>();
            job.JobTechnologicalProcess.Add("车站人员盯控签认");
            job.JobTechnologicalProcess.Add("作业前登记签认");
            job.JobTechnologicalProcess.Add("防护员签认");
            job.JobTechnologicalProcess.Add("时间审核");
            job.JobTechnologicalProcess.Add("销记施工");
            //添加流程色
            String color = ColorTranslator.ToHtml(Color.LightGreen);
            job.JobTProcessColor = new List<string>();
            job.JobTProcessColor.Add(color);
            job.JobTProcessColor.Add(color);
            job.JobTProcessColor.Add(color);
            job.JobTProcessColor.Add(color);
            job.JobTProcessColor.Add(color);

            DataHelper.GetInstance().JobList.Add(job);
            bool isOk = XmlHelper.ObjectToXMLFile(DataHelper.GetInstance().JobList, ".\\JobModelList.xml", Encoding.Unicode);

            if (isOk)
            {
                MessageBox.Show("添加成功");
            }else
            {
                MessageBox.Show("添加失败");
            }
            
        }
         /// <summary>  
         /// 根据GUID获取16位的唯一字符串  
         /// </summary>  
         /// <param name=\"guid\"></param>  
         /// <returns></returns>  
         public static string GuidTo16String()
         {
             long i = 1;
             foreach (byte b in Guid.NewGuid().ToByteArray())
                 i *= ((int) b + 1);
             return string.Format("{0:x}", i - DateTime.Now.Ticks);
         }

        private void cbBNature_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbBNature.Text.Trim() == "施工")
            {
                tBxJobindex.Enabled = true;
            }
            else
            {
                tBxJobindex.Enabled = false;
            }
        }
    }
}
